/*
 * @Author: boyyang
 * @Date: 2022-01-10 17:48:15
 * @LastEditTime: 2022-03-18 21:58:02
 * @LastEditors: boyyang
 * @Description: 
 * @FilePath: \boyyang\src\cloudbase\db.ts
 */

import { db, app, auth } from './init'
import cloudbase from '@cloudbase/js-sdk'

/**
 * @description: 
 * @param {any} filePath 上传文件
 * @param {string} collectionName 集合名称
 * @param {string} cloudPath 云端文件名称
 * @return {*}
 */
const upload = (filePath: any, collectionName: string = 'imagesUpload', cloudPath: string = 'cloudFile'): Promise<any> => {
    return new Promise((resolve, reject) => {
        // 当前用户信息
        const userInfo = auth.currentUser
        // 云端文件存储路径
        const path = `${userInfo?.email}/${cloudPath}/${filePath.name}`
        app
            .uploadFile({
                cloudPath: path,
                filePath
            })
            .then((res: cloudbase.storage.ICloudbaseUploadFileResult) => {
                const fileId = res.fileID
                app
                    .getTempFileURL({
                        fileList: [{
                            fileID: fileId,
                            maxAge: 24 * 60 * 60 * 1000
                        }]
                    })
                    .then((res: any) => {
                        const fileList = res.fileList
                        const time = new Date()
                        let addParams = {
                            email: userInfo?.email,
                            uid: userInfo?.uid,
                            download_url: fileList[0].download_url,
                            tempFileURL: fileList[0].tempFileURL,
                            fileID: fileList[0].fileID,
                            filePath: path,
                            create_time: time.getTime(),
                            name: filePath.name,
                            isShow: true
                        }
                        db
                            .collection(collectionName)
                            .add(addParams)
                            .then((res) => {
                                resolve(addParams)
                            })
                    })
                    .catch((err) => {
                        reject(err)
                    })
            })
            .catch((err) => {
                reject(err)
            })
    })

}

/**
 * @description: 获取图片
 * @param {string} collectionName
 * @param {number} page
 * @param {number} limit
 * @return {*}
 */
const getImgs = (collectionName: string, page: number = 0, limit: number = 6, order = { key: "id", type: "desc" }, query = {}): Promise<cloudbase.database.GetRes> => {
    return db
        .collection(collectionName)
        .where(query)
        .orderBy(order.key, order.type)
        .skip(page * limit)
        .limit(limit)
        .get()
}

/**
 * @description: 删除数据
 * @param {string} collectionName
 * @param {string} id
 * @return {*}
 */
const delData = (collectionName: string, id: string) => {
    return db
        .collection(collectionName)
        .doc(id).remove()
}

/**
 * @description: 获取集合数据
 * @param {string} collectionName
 * @param {number} page
 * @param {number} limit
 * @return {*}
 */
const getData = (collectionName: string, page: number = 0, limit: number = 6, order = { key: "id", type: "desc" }): Promise<cloudbase.database.GetRes> => {
    return db
        .collection(collectionName)
        .orderBy(order.key, order.type)
        .skip(page * limit)
        .limit(limit)
        .get()
}

/**
 * @description: 通过 id 查询数据
 * @param {string} collectionName
 * @param {string} id
 * @return {*}
 */
const findDataById = (collectionName: string, id: string) => {
    return db
        .collection(collectionName)
        .doc(id)
        .get()
}

/**
 * @description: 通过 _openid 查询数据
 * @param {string} collectionName
 * @param {string} id
 * @return {*}
 */
const findDataByOpenId = (collectionName: string, id: string) => {
    return db
        .collection(collectionName)
        .where({ '_openid': id })
        .get()
}

/**
 * @description: 添加一条数据
 * @param {string} collectionName
 * @param {object} data
 * @return {*}
 */
const addOneData = (collectionName: string, data: object) => {
    return db
        .collection(collectionName)
        .add({
            ...data
        })
}

/**
 * @description: 更新一条数据
 * @param {string} collectionName
 * @param {string} id
 * @param {object} data
 * @return {*}
 */
const updateOneData = (collectionName: string, id: string, data: object) => {
    return db
        .collection(collectionName)
        .doc(id)
        .update(data)
}

/**
 * @description: 通过 _openid 更新数据
 * @param {string} collectionName
 * @param {string} id
 * @param {object} data
 * @return {*}
 */
const updateOneDataByOpenId = (collectionName: string, id: string, data: object) => {
    return db
        .collection(collectionName)
        .where({ '_openid': id })
        .update(data)
}

export {
    upload,
    getImgs,
    delData,
    getData,
    findDataById,
    findDataByOpenId,
    addOneData,
    updateOneData,
    updateOneDataByOpenId
}
